package com.smart.campus.school.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.common.utils.StringUtils;
import com.smart.campus.pay.WXPay;
import com.smart.campus.pay.WXPayUtil;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.domain.request.PaymentBillRequest;
import com.smart.campus.school.mapper.StudentMapper;
import com.smart.campus.school.service.IMsgService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.ScPaymentBillMapper;
import com.smart.campus.school.domain.ScPaymentBill;
import com.smart.campus.school.service.IScPaymentBillService;
import com.smart.campus.common.core.text.Convert;

import javax.servlet.ServletOutputStream;

/**
 * 缴费单Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-05-13
 */
@Service
public class ScPaymentBillServiceImpl implements IScPaymentBillService 
{
    @Autowired
    private ScPaymentBillMapper scPaymentBillMapper;


    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private IMsgService msgService;

    /**
     * 查询缴费单
     * 
     * @param id 缴费单ID
     * @return 缴费单
     */
    @Override
    public ScPaymentBill selectScPaymentBillById(Long id)
    {
        return scPaymentBillMapper.selectScPaymentBillById(id);
    }

    /**
     * 查询缴费单列表
     * 
     * @param scPaymentBill 缴费单
     * @return 缴费单
     */
    @Override
    public List<ScPaymentBill> selectScPaymentBillList(ScPaymentBill scPaymentBill)
    {
        return scPaymentBillMapper.selectScPaymentBillList(scPaymentBill);
    }

    /**
     * 新增缴费单
     * 
     * @param request 缴费单
     * @return 结果
     */
    @Override
    public int insertScPaymentBill(PaymentBillRequest request)
    {
        if(request.getSendType() == 1){
            Student param = new Student();
            param.setGradeId(request.getGradeId());
            param.setStatus(1);
            List<Student> studentList = studentMapper.selectStudentList(param);

            for (Student student : studentList) {
                ScPaymentBill scPaymentBill = new ScPaymentBill();
                BeanUtils.copyProperties(request,scPaymentBill);

                scPaymentBill.setUserId(student.getId());
                scPaymentBill.setUserName(student.getStudentName());
                scPaymentBill.setCreateTime(DateUtils.getNowDate());
                scPaymentBill.setStatus(1);
                scPaymentBillMapper.insertScPaymentBill(scPaymentBill);
                Msg msg = new Msg();
                msg.setUserType(2);
                msg.setStatus(0);
                msg.setCreateTime(new Date());
                msg.setUserId(student.getId());
                msg.setUserName(student.getStudentName());
                msg.setContent("您有待付款的账单需要处理!");
                msg.setMsgType(3);
                msg.setMsgLink("/student/pay/bill");
                msgService.insertMsg(msg);
            }
        }else {

            String userIds = request.getUserIds();
            if(StringUtils.isNotEmpty(userIds)){
                String[] userIdArray = userIds.split(",");
                String[] userNameArray = request.getUserNames().split(",");

                for (int i = 0; i < userIdArray.length; i++) {
                    ScPaymentBill scPaymentBill = new ScPaymentBill();
                    BeanUtils.copyProperties(request,scPaymentBill);
                    scPaymentBill.setCreateTime(DateUtils.getNowDate());
                    scPaymentBill.setStatus(0);//待缴费
                    scPaymentBill.setUserId(Long.valueOf(userIdArray[i]));
                    scPaymentBill.setUserName(userNameArray[i]);
                    scPaymentBillMapper.insertScPaymentBill(scPaymentBill);
                    Msg msg = new Msg();
                    msg.setUserType(2);
                    msg.setStatus(0);
                    msg.setCreateTime(new Date());
                    msg.setUserId(Long.valueOf(userIdArray[i]));
                    msg.setUserName(userNameArray[i]);
                    msg.setContent("您有待付款的账单需要处理!");
                    msg.setMsgType(3);
                    msg.setMsgLink("/student/pay/bill");
                    msgService.insertMsg(msg);
                }

            }

        }

        return 1;
    }

    /**
     * 修改缴费单
     * 
     * @param scPaymentBill 缴费单
     * @return 结果
     */
    @Override
    public int updateScPaymentBill(ScPaymentBill scPaymentBill)
    {
        scPaymentBill.setUpdateTime(DateUtils.getNowDate());
        return scPaymentBillMapper.updateScPaymentBill(scPaymentBill);
    }

    /**
     * 删除缴费单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteScPaymentBillByIds(String ids)
    {
        return scPaymentBillMapper.deleteScPaymentBillByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除缴费单信息
     * 
     * @param id 缴费单ID
     * @return 结果
     */
    @Override
    public int deleteScPaymentBillById(Long id)
    {
        return scPaymentBillMapper.deleteScPaymentBillById(id);
    }


    @Override
    public void createPayCode() {

    }

    @Override
    public ScPaymentBill selectScPaymentBillByPaymentNo(String tradeNo) {
        return scPaymentBillMapper.selectScPaymentBillByPaymentNo(tradeNo);
    }


}
